﻿using LanguagePolish.Base;
using LanguagePolish.EntityModel;
using LanguagePolish.Models;
using LanguagePolish.Views;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using System.Windows.Threading;

namespace LanguagePolish.ViewModels
{
    class ClassroomsViewModel : BaseViewModel
    {
        private ClassroomsModel model;

        private DispatcherTimer timer;


        private ObservableCollection<Classroom> classrooms;

        public ObservableCollection<Classroom> Classrooms { get { return classrooms; } }

        public ICommand AddClassroomCommand { get; private set; }
        public ICommand EditClassroomCommand { get; private set; }
        public ICommand RemoveClassroomCommand { get; private set; }
        public ICommand ShowInfoCommand { get; private set; }

        public ClassroomsViewModel()
        {
            model = new ClassroomsModel();
            AddClassroomCommand = new RelayCommand(AddNewClassroom, (o) => model.CanUserAddClassrooms);
            EditClassroomCommand = new RelayCommand((o) => EditClassroom((Classroom)o), (o) => model.CanUserEditClassrooms);
            RemoveClassroomCommand = new RelayCommand((o) => RemoveClassroom((Classroom)o), (o) => model.CanUserRemoveClassrooms);
            ShowInfoCommand = new RelayCommand((o) => ShowClassroomInfo((Classroom)o));
            timer = new DispatcherTimer();
            timer.Interval = new TimeSpan(0, 0, 0, 0, 300);
            timer.Tick += timer_RefreshFilter;

            UpdateClassroomCollection();
        }

        private void UpdateClassroomCollectionWithWaiting()
        {
            timer.Stop();
            timer.Start();
        }

        private void UpdateClassroomCollection()
        {
            var res = model.GetClassrooms();
            classrooms = new ObservableCollection<Classroom>(res);
            OnPropertyChanged("Classrooms");
            timer.Stop();
        }

        void timer_RefreshFilter(object sender, EventArgs e)
        {
            UpdateClassroomCollection();
        }

        private void AddNewClassroom()
        {
            Classroom classroom = new Classroom();
            AddClassroomView addClassroomDialog = new AddClassroomView(classroom);
            bool? res = addClassroomDialog.ShowDialog();
            if (res == true)
            {
                classrooms.Add(classroom);      // добавляем новый кабинет без обновления свойства.
                model.AddClassroom(classroom);
                LinksProvider.Model.CollectionsHasBeenChanged();
            }
        }

        private void EditClassroom(Classroom classroom)
        {
            AddClassroomView addClassroomDialog = new AddClassroomView(classroom, true);
            bool? res = addClassroomDialog.ShowDialog();
            if (res == true)
            {
                classroom.NotifyAll();    // обновляем все свойства
                model.SaveChanges();
                LinksProvider.Model.CollectionsHasBeenChanged();
            }
            else
            {
                model.RollbackClassroom(classroom);     // откат изменений
            }
        }

        private void RemoveClassroom(Classroom classroom)
        {
            MessageBoxResult res = MessageBox.Show("Delete this Classroom?", "Attention", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (res == MessageBoxResult.Yes)
            {
                model.DeleteClassroom(classroom);
                classrooms.Remove(classroom);       // из каждой коллекции удаляем отдельно
                LinksProvider.Model.CollectionsHasBeenChanged();
            }
        }
        private void ShowClassroomInfo(Classroom classroom)
        {
            ClassroomInfoView infoDialog = new ClassroomInfoView(classroom);
            infoDialog.ShowDialog();
        }
    }
}
